package liheng.vue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import liheng.vue.constant.Constant;
import liheng.vue.dao.EmpDao;
import liheng.vue.entity.Emp;
import liheng.vue.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpDao empDao;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<Emp> find(Integer pageNo, Integer pageSize, Emp emp) {
        PageHelper.startPage(pageNo,pageSize);


        LambdaQueryWrapper<Emp> empWrapper=new LambdaQueryWrapper<Emp>();

        if(emp.getName()!=null&&!"".equals(emp.getName().trim())){
            empWrapper.eq(Emp::getName,emp.getName());
        }

//        if(emp.getName()!=null&&!"".equals(emp.getName().trim())){
//            empWrapper.eq(Emp::getName,emp.getName());
//        }

        if(emp.getSalary()!=null){
            empWrapper.eq(Emp::getSalary,emp.getSalary());
        }


        List<Emp> emps=empDao.selectList(empWrapper);

        return new PageInfo<>(emps);
    }

    @Override
    public void add(Emp emp) {
        emp.setStatus(Constant.EmpStatus.Emp_STATUS_ENABLE);
        empDao.insert(emp);
    }

    @Override
    public void remove(Integer id) {
        empDao.deleteById(id);
    }

    @Override
    public void modifyStatus(Integer id, String status) {
        LambdaUpdateWrapper<Emp> empWrapper=new LambdaUpdateWrapper<>();
        empWrapper.set(Emp::getStatus,status);
        empWrapper.eq(Emp::getId,id);
        empDao.update(null,empWrapper);
    }

    @Override
    public void modify(Emp emp) {
        empDao.updateById(emp);
    }
}
